import xlrd

import xlwt
import random
import xlutils
import lxml
import pandas as pd
from selenium import webdriver
import time
from bs4 import BeautifulSoup
import requests
from requests.cookies import RequestsCookieJar
import urllib3
import re

# 自动登录
drive = webdriver.Chrome()
drive.get('https://aregsr.vip/')
drive.find_element_by_id('goin').click()
drive.find_element_by_id('ls_username').send_keys('ggfygg')
drive.find_element_by_id('ls_password').send_keys('fhrAKMv@A4NXLkN')
drive.find_element_by_class_name('mem_login').click()
time.sleep(2)

# selenium保存cookies
import json

cookies = drive.get_cookies()
with open("cookies.txt", "w") as fp:
    json.dump(cookies, fp)

# requests读取cookies

urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)

s = requests.session()
s.verify = False
headers = {
    "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.4389.90 Safari/537.36"}
# s.get("http://www.baidu.com")

# 这里我们使用cookie对象进行处理
jar = RequestsCookieJar()
with open("cookies.txt", "r") as fp:
    cookies = json.load(fp)
    for cookie in cookies:
        jar.set(cookie['name'], cookie['value'])

# # 或者如果你只是想创建一张空表
workbook = xlwt.Workbook(encoding='utf-8')
# # 创建一个sheet
worksheet = workbook.add_sheet('sheet')

# # 获取一个已存在的sheet
# worksheet = workbook.get_sheet('sheet')
df = pd.read_excel("test6.xls", header=None)

url = df[0].tolist()
for i in range(len(url)):
    print(url[i])

    r = s.get(url[i], headers=headers, cookies=jar)
    r.encoding = "utf-8"

    # res = r.text
    # print(res)
    # gZu2\(b1I0-Dc&3t6|：8888|：1024|g2j0s4g8rhs|：1314|：118|gZu2(b1I0-Dc&amp;8t5|：2046|hao9ding.xia
    try:
        pattern1 = re.compile(r'3t6')
        result1 = re.search(pattern1, r.text)

        pattern2 = re.compile(r'：8888')
        result2 = re.search(pattern2, r.text)

        pattern3 = re.compile(r'：1024')
        result3 = re.search(pattern3, r.text)

        pattern4 = re.compile(r'：2046')
        result4 = re.search(pattern4, r.text)

        pattern5 = re.compile(r'g2j0s4g8rhs')
        result5 = re.search(pattern5, r.text)

        pattern6 = re.compile(r'8t5')
        result6 = re.search(pattern6, r.text)

        pattern7 = re.compile(r'：118')
        result7 = re.search(pattern7, r.text)

        pattern19 = re.compile(r'sex8.cc')
        result19 = re.search(pattern19, r.text)


        pattern16 = re.compile(r'【解压密码】：(.*)')
        result16 = re.search(pattern16, r.text)

        # pattern = re.compile(r'：8888')
        # result = re.match(pattern2, '：8888')
        # pattern = re.compile(r'：118')
        # result = re.match(pattern3, '：118')
        if result1:
            # 获取一个已存在的sheet
            # worksheet = workbook.get_sheet('sheet')
            # # 写入一个值，括号内分别为行数、列数、内容
            worksheet.write(i, 1, 'gZu2(b1I0-Dc&3t6')
            print('1成功')
        else:
            if result2:
                worksheet.write(i, 1, '8888')
                print('2成功')
            else:
                if result3:
                    worksheet.write(i, 1, '1024')
                    print('3成功')
                else:
                    if result4:
                        worksheet.write(i, 1, '2046')
                        print('4成功')
                    else:
                        if result5:
                            worksheet.write(i, 1, 'g2j0s4g8rhs')
                            print('5成功')
                        else:
                            if result6:
                                worksheet.write(i, 1, 'gZu2(b1I0-Dc&8t5')
                                print('6成功')
                            else:
                                if result7:
                                    worksheet.write(i, 1, '118')
                                    print('7成功')
                                else:
                                    if result19:
                                        worksheet.write(i, 1, result19.group())
                                        print('19成功')
                                    else:
                                        if result16:
                                            worksheet.write(i, 1, result16.group())
                                            print('16成功')
                                        else:
                                            print('没有密码')



        pattern8 = re.compile(r'http://www.xun-niu.com/(.*?)html')
        result8 = re.search(pattern8, r.text)


        if result8:
            # 获取一个已存在的sheet
            # worksheet = workbook.get_sheet('sheet')
            # # 写入一个值，括号内分别为行数、列数、内容
            worksheet.write(i, 2, result8.group())
            print('8成功')
        else:
            pattern9 = re.compile(r'http://www.ibuspan.com/(.*?)html')
            result9 = re.search(pattern9, r.text)

            if result9:
                worksheet.write(i, 2, result9.group())
                print('9成功')
            else:
                pattern10 = re.compile(r'http://www.onstclouds.com/(.*?)html')
                result10 = re.search(pattern10, r.text)

                if result10:
                    worksheet.write(i, 2, result10.group())
                    print('10成功')
                else:
                    pattern11 = re.compile(r'https://www.567yunpan.com/(.*?)html')
                    result11 = re.search(pattern11, r.text)

                    if result11:
                        worksheet.write(i, 2, result11.group())
                        print('11成功')
                    else:
                        pattern12 = re.compile(r'http://www.buswok.com/(.*?)html')
                        result12 = re.search(pattern12, r.text)

                        if result12:
                            worksheet.write(i, 2, result12.group())
                            print('12成功')
                        else:
                            pattern13 = re.compile(r'http://www.xueqiupan.com/(.*?)html')
                            result13 = re.search(pattern13, r.text)

                            if result13:
                                worksheet.write(i, 2, result13.group())
                                print('13成功')
                            else:
                                pattern14 = re.compile(r'http://www.expfile.com/(.*?)html')
                                result14 = re.search(pattern14, r.text)

                                if result14:
                                    worksheet.write(i, 2, result14.group())
                                    print('14成功')
                                else:
                                    pattern15 = re.compile(r'http://www.tadaigou.com/(.*?)html')
                                    result15 = re.search(pattern15, r.text)

                                    if result15:
                                        worksheet.write(i, 2, result15.group())
                                        print('15成功')
                                    else:
                                        pattern17 = re.compile(r'http://share4uspace.com(.*?)"')
                                        result17 = re.search(pattern17, r.text)
                                        if result17:
                                            worksheet.write(i, 2, result17.group())
                                            print('17成功')
                                        else:
                                            pattern18 = re.compile(r'http://www.upfilex.com/(.*?)html')
                                            result18 = re.search(pattern18, r.text)
                                            if result18:
                                                worksheet.write(i, 2, result18.group())
                                                print('18成功')
                                            else:
                                                print('没有url')
        time.sleep(random.randint(2, 5))


    except:
        workbook.save('mm+url-6.xls')


workbook.save('mm+url-6.xls')



















